from django.shortcuts import render
from django.http import HttpResponse
from datetime import datetime

def index(request):
    response = HttpResponse('index')
    expires = datetime(year=2021,month=5,day=13,hour=20,minute=0,second=0)
    # response.set_cookie(key='username',value='zhiliao',max_age=180)
    response.set_cookie(key='user_id',value='abc',expires=expires)
    return response

def delect_cookies(request):
    response = HttpResponse('delect')
    response.delete_cookie('user_id')
    return response

def my_list(request):
    cookies = request.COOKIES
    username = cookies.get('user_id')
    return HttpResponse(username)

def cms_view(request):
    cookies = request.COOKIES
    username = cookies.get('user_id')
    return HttpResponse(username)

def session_view(request):
    request.session['username'] = 'zhiliao'
    # username = request.session.pop('username')  # 删除session
    username = request.session.get('username')
    # request.session['username']= 'zhiliao'
    # request.session['userid']= '10'
    # request.session.clear()   # 清除当前这个用户的session数据
    # request.session.flush()     # 删除session数据，并删除浏览器中存储的session，一般在注销时候用的多
    # print(username)
    # request.session.set_expiry(0)     # 设置过期时间      # 0代表关闭浏览器过期 None大约2周时间
    # request.session.set_expiry(-1)
    # request.session.clear_expired()     # 删除过期session
    return HttpResponse('session view')

